package com.cmcc.groupcontacts.firewall.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmcc.groupcontacts.common.PreferenceManager;
import com.cmcc.groupcontacts.firewall.activity.strategy.WindowTitleStrategy;
import com.yinhai.activity.R;

/**
 * 自定义拦截模式设置Activity
 * 
 * @author
 * 
 */
public class FirewallConfigModeCustomActivity extends BaseActivity {
	/** 黑名单 */
	public static final int MODE_BLACK = 0;
	/** 白名单 */
	public static final int MODE_WHITE = 1;
	/** 本地联系人电话 */
	public static final int MODE_LOCAL_CONTACTS_CALL = 2;
	/** 本地联系人短信 */
	public static final int MODE_LOCAL_CONTACTS_SMS = 3;
	/** 集团联系人电话 */
	public static final int MODE_GROUP_CONTACTS_CALL = 4;
	/** 集团联系人短信 */
	public static final int MODE_GROUP_CONTACTS_SMS = 5;
	/** 陌生联系人电话 */
	public static final int MODE_STRANGER_CALL = 6;
	/** 陌生联系人短信 */
	public static final int MODE_STRANGER_SMS = 7;
	/** 骚扰电话 */
	public static final int MODE_DISTURB_CALL = 8;

	@Override
	public void setTitleStrategy() {
		titleStrategy = new WindowTitleStrategy(this);
	}

	@Override
	public void setTitleButtons() {
		// 左边的“返回”按钮
		getLeftLayout().addView(createBackButton());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firewall_config_mode_custom_activity);

		init();
	}

	private void init() {
		setItem(findViewById(R.id.lay_mode_customize_edit_black),
				findViewById(R.id.txt_mode_customize_edit_black),
				findViewById(R.id.txt_mode_customize_edit_black_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_BLACK, true);
		setItem(findViewById(R.id.lay_mode_customize_edit_white),
				findViewById(R.id.txt_mode_customize_edit_white),
				findViewById(R.id.txt_mode_customize_edit_white_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_WHITE, true);
		setItem(findViewById(R.id.lay_mode_customize_edit_local_contacts_call),
				findViewById(R.id.txt_mode_customize_edit_local_contacts_call),
				findViewById(R.id.txt_mode_customize_edit_local_contacts_call_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_LOCAL_CONTACTS_CALL,
				false);
		setItem(findViewById(R.id.lay_mode_customize_edit_local_contacts_sms),
				findViewById(R.id.txt_mode_customize_edit_local_contacts_sms),
				findViewById(R.id.txt_mode_customize_edit_local_contacts_sms_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_LOCAL_CONTACTS_SMS,
				false);
		setItem(findViewById(R.id.lay_mode_customize_edit_group_contacts_call),
				findViewById(R.id.txt_mode_customize_edit_group_contacts_call),
				findViewById(R.id.txt_mode_customize_edit_group_contacts_call_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_GROUP_CONTACTS_CALL,
				false);
		setItem(findViewById(R.id.lay_mode_customize_edit_group_contacts_sms),
				findViewById(R.id.txt_mode_customize_edit_group_contacts_sms),
				findViewById(R.id.txt_mode_customize_edit_group_contacts_sms_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_GROUP_CONTACTS_SMS,
				false);
		setItem(findViewById(R.id.lay_mode_customize_edit_stranger_call),
				findViewById(R.id.txt_mode_customize_edit_stranger_call),
				findViewById(R.id.txt_mode_customize_edit_stranger_call_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_STRANGER_CALL, false);
		// TODO: 更改为拦截/智能拦截/放行3种状态
		setItem(findViewById(R.id.lay_mode_customize_edit_stranger_sms),
				findViewById(R.id.txt_mode_customize_edit_stranger_sms),
				findViewById(R.id.txt_mode_customize_edit_stranger_sms_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_STRANGER_SMS, true);
		// TODO: 自定义骚扰响铃时长
		setItem(findViewById(R.id.lay_mode_customize_edit_disturb_call),
				findViewById(R.id.txt_mode_customize_edit_disturb_call),
				findViewById(R.id.txt_mode_customize_edit_disturb_call_switch),
				PreferenceManager.CONFIG_FIREWALL_MODE_DISTURB_CALL, true);
	}

	/*
	 * 设置拦截/放行状态显示和单击切换事件
	 */
	private void setItem(final View layItem, final View txtItem,
			final View txtSwitchItem, final String modeKey,
			final boolean defaultState) {
		setItemState(txtItem, txtSwitchItem, modeKey,
				PreferenceManager.getFirewallModeState(modeKey, defaultState));
		layItem.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				boolean state = !PreferenceManager.getFirewallModeState(
						modeKey, defaultState);
				setItemState(txtItem, txtSwitchItem, modeKey, state);
				PreferenceManager.setFirewallModeState(modeKey, state);
			}
		});
	}

	/*
	 * 设置TextView项的拦截/放行状态显示
	 */
	private void setItemState(View txtItem, View txtSwitchItem, String modeKey,
			boolean state) {
		if (state) {
			Drawable imgOn = getDrawable(R.drawable.ic_switch_open_blank);
			imgOn.setBounds(0, 0, imgOn.getMinimumWidth(),
					imgOn.getMinimumHeight());
			((TextView) txtItem).setCompoundDrawables(null, null, imgOn, null);

			ViewGroup.LayoutParams paramsOn = txtSwitchItem.getLayoutParams();
			paramsOn.width = getDmConverter().DipToPixel(82);
			txtSwitchItem.setLayoutParams(paramsOn);
			if (modeKey.equals(PreferenceManager.CONFIG_FIREWALL_MODE_WHITE)) {
				((TextView) txtSwitchItem).setText("放行");
			} else {
				((TextView) txtSwitchItem).setText("拦截");
			}
		} else {
			Drawable imgOff = getDrawable(R.drawable.ic_switch_close_blank);
			imgOff.setBounds(0, 0, imgOff.getMinimumWidth(),
					imgOff.getMinimumHeight());
			((TextView) txtItem).setCompoundDrawables(null, null, imgOff, null);

			ViewGroup.LayoutParams paramsOff = txtSwitchItem.getLayoutParams();
			paramsOff.width = getDmConverter().DipToPixel(46);
			txtSwitchItem.setLayoutParams(paramsOff);
			if (modeKey.equals(PreferenceManager.CONFIG_FIREWALL_MODE_BLACK)
					|| modeKey
							.equals(PreferenceManager.CONFIG_FIREWALL_MODE_WHITE)) {
				((TextView) txtSwitchItem).setText("关闭");
			} else {
				((TextView) txtSwitchItem).setText("放行");
			}
		}
	}
}
